package com.nlscan.qdkit.ui.publico.util

import android.view.animation.Animation
import android.view.animation.ScaleAnimation
import android.view.animation.TranslateAnimation

object AnimationUtil {
    const val DEFAULT_TIME = 500

    fun getHorizontalAnimation(start: Float, end: Float, durationMillis: Int): Animation {
        val translateAnimation: Animation = TranslateAnimation(
            Animation.RELATIVE_TO_PARENT,
            start,
            Animation.RELATIVE_TO_PARENT,
            end,
            Animation.RELATIVE_TO_PARENT,
            0F,
            Animation.RELATIVE_TO_PARENT,
            0F
        )
        translateAnimation.duration = durationMillis.toLong()
        return translateAnimation
    }

    fun createTranslateAnimation(fromX: Float, toX: Float, fromY: Float, toY: Float): Animation {
        val animation: Animation = TranslateAnimation(
            Animation.RELATIVE_TO_SELF,
            fromX,
            Animation.RELATIVE_TO_SELF,
            toX,
            Animation.RELATIVE_TO_SELF,
            fromY,
            Animation.RELATIVE_TO_SELF,
            toY
        )
        animation.duration = 500
        return animation
    }

    fun createScaleAnimation(
        fromX: Float,
        toX: Float,
        fromY: Float,
        toY: Float,
        pivotXValue: Float,
        pivotYValue: Float
    ): Animation {
        val scaleAnimation: Animation = ScaleAnimation(
            fromX,
            toX,
            fromY,
            toY,
            Animation.RELATIVE_TO_SELF,
            pivotXValue,
            Animation.RELATIVE_TO_SELF,
            pivotYValue
        )
        scaleAnimation.duration = 500
        return scaleAnimation
    }

    fun getTranslateVerticalAnimation(start: Float, end: Float, durationMillis: Int): Animation {
        val translateAnimation: Animation = TranslateAnimation(
            Animation.RELATIVE_TO_PARENT,
            0F,
            Animation.RELATIVE_TO_PARENT,
            0F,
            Animation.RELATIVE_TO_PARENT,
            start,
            Animation.RELATIVE_TO_PARENT,
            end
        )
        translateAnimation.duration = durationMillis.toLong()
        return translateAnimation
    }

    object In {
        fun ScaleInFormLeft(): Animation {
            return createScaleAnimation(0f, 1f, 1f, 1f, 0f, 0.5f)
        }

        fun ScaleInFormRight(): Animation {
            return createScaleAnimation(0f, 1f, 1f, 1f, 1f, 0.5f)
        }

        fun ScaleInFormTop(): Animation {
            return createScaleAnimation(1f, 1f, 0f, 1f, 0.5f, 0f)
        }

        fun ScaleInFormBottom(): Animation {
            return createScaleAnimation(1f, 1f, 0f, 1f, 0.5f, 1f)
        }

        fun TranslateFromTop(time: Int = DEFAULT_TIME): Animation {
            return getTranslateVerticalAnimation(-1f, 0f, time)
        }

        fun TranslateFromBottom(time: Int = DEFAULT_TIME): Animation {
            return getTranslateVerticalAnimation(1f, 0f, time)
        }

        fun TranslateFromLeft(time: Int = DEFAULT_TIME): Animation {
            return getHorizontalAnimation(-1f, 0f, time)
        }

        fun TranslateFromRight(time: Int = DEFAULT_TIME): Animation {
            return getHorizontalAnimation(1f, 0f, time)
        }
    }

    object Out {
        fun ScaleOutFormLeft(): Animation {
            return createScaleAnimation(1f, 0f, 1f, 1f, 0f, 0.5f)
        }

        fun ScaleOutFormRight(): Animation {
            return createScaleAnimation(1f, 0f, 1f, 1f, 1f, 0.5f)
        }

        fun ScaleOutFormTop(): Animation {
            return createScaleAnimation(1f, 1f, 1f, 0f, 0.5f, 0f)
        }

        fun ScaleOutFormBottom(): Animation {
            return createScaleAnimation(1f, 1f, 1f, 0f, 0.5f, 1f)
        }

        fun TranslateFromTop(time: Int = DEFAULT_TIME): Animation {
            return getTranslateVerticalAnimation(0f, -1f, time)
        }

        fun TranslateFromBottom(time: Int = DEFAULT_TIME): Animation {
            return getTranslateVerticalAnimation(0f, 1f, time)
        }

        fun TranslateFromLeft(time: Int = DEFAULT_TIME): Animation {
            return getHorizontalAnimation(0f, -1f, time)
        }

        fun TranslateFromRight(time: Int = DEFAULT_TIME): Animation {
            return getHorizontalAnimation(0f, -1f, time)
        }
    }
}